home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 751 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
  2. Message-ID: <314CD29A.3438@cs.tu-berlin.de>
  3. X-Original-Date: Mon, 18 Mar 1996 04:03:54 +0100
  4. Path: in2.uu.net!bounce-back
  5. Date: 18 Mar 96 08:02:41 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Constructors and conversion operator
  9. Organization: Technical University of Berlin
  10. X-Mailer: Mozilla 2.0 (Win95; I)
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMU0YxuEDnX0m9pzZAQEGDwF9GG0npd/5JHLBtEZnio3leXumNS6zkLnX
  13.     uZ+X1HQPJHITH8vMzj3VCcfKS6PkAXXE
  14.     =hj4C
  15.  
  16. Hi,
  17.  
  18. I have a question about conversion operators and constructors. Consider the 
  19. following:
  20.  
  21. class X
  22. {
  23.     X( Y );
  24. };
  25.  
  26. class Y
  27. {
  28.     Y();
  29.  
  30.     operator X();
  31. };
  32.  
  33. void foo( X );
  34.  
  35. int main()
  36. {
  37.     Y y;
  38.     foo1(y);
  39.     foo1(X(y));
  40.     foo1((X)y);
  41. };
  42.  
  43. In all three cases my compiler says that the call is ambiguous. Is it 
  44. correct? The DWP says:
  45.  
  46.     User-defined conversions are used implicitly only if they are 
  47. unambiguous. [class.conv.fct]
  48.  
  49. What is meant by user-defined conversions - only conversion operators or both 
  50. conversion operator and converting constructors?
  51.  
  52. Now the second one:
  53.  
  54. class A
  55. {
  56.         A(B);
  57. };
  58.  
  59. class B
  60. {
  61.         operator A&();
  62. };
  63.  
  64.  
  65. void foo( A )
  66. {}
  67.  
  68. int main()
  69. {
  70.     B b;
  71.  
  72.     foo( b );
  73.     foo(A(b));
  74.     foo((A)b);
  75. }
  76.  
  77. Again, my compiler complains about all three calls. In fact, it complains 
  78. even about A a = A(b). Is this correct?
  79.  
  80. Bye
  81.  
  82. Roman
  83. ---
  84. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  85. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  86. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  87. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  88. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  89.